const db = require('../utils/db.js')

const addScore = (req, res) => {
  const { student_id, course_id, num } = req.body
  console.log(req.body);
  const sql = `select * from t_score where student_id=? and course_id=?`;
  db.query(sql, [student_id, course_id], (err, results) => {
    if (err) return res.cc(err)
    if (results.length > 0) {
      return res.ok(null, "该记录已经存在")
    }
    const sql = `insert into t_score set ?`
    db.query(sql, req.body, (err, results) => {
      if (err) return res.cc(err)
      if (results.affectedRows == 1) {
        return res.ok(null, "添加成功")
      }
      res.cc("添加失败，请稍后重试")
    })
  })
}

// 查询
const findAll = (req, res) => {
  let sqlc = ``
  const { student_id, c_id } = req.body
  console.log(req.body);
  if (student_id) {
    sqlc = ` and ts.id = ?`
  } else if (c_id) {
    sqlc = ` and ts.c_id = ?`
  }
  // const sql = ` SELECT s.*,tc.className FROM t_class tc,
  //             ( SELECT te.*,ts.*,tc.name AS courseName FROM 
  //             t_score te, t_course tc,t_student ts 
  //             WHERE te.student_id=ts.id AND te.course_id=tc.id ${sqlc})
  //              AS s 
  //              WHERE tc.id = s.c_id`
  const sql = `select ts.*,tc.className from t_class tc,t_student ts where ts.c_id = tc.id ${sqlc}`
  db.query(sql, student_id ? student_id : c_id, async (err, results) => {
    if (err) return res.cc(err)
    // for(let i in results){ // 这个能成功
    //   const data = await selectStudentScore(results[i].id)
    //   results[i].score = data
    // }
    // 利用map遍历返回一个promise数组 将所有promise对象转到成功状态即可
    // 等待 返回promise数组
     const data = await results.map(async element => { // async修饰的函数 默认返回值会被一个promise对象包裹
      const data = await selectStudentScore(element.id); // await自动将promise对象.then 拿到返回的值
      element.score = data 
    });
    await Promise.all(data) // 调用 执行
    // console.log(results);
    return res.ok(results)
  })
}

const selectStudentScore = (id) => {
  return new Promise((rel, rej) => {
    const sql = `select te.*,tc.name as courseName from t_score te,t_course tc where te.course_id=tc.id and te.student_id = ?`
    db.query(sql, id, (err, results) => {
      if (err) return rej(err)
      // console.log(results);
      rel(results)
    })
  })
}

const updateScore = (req, res) => {

  console.log(req.body);
  const { num, student_id, course_id } = req.body
  const sql = `update t_score set num=? where student_id=? and course_id = ?`
  db.query(sql, [num, student_id, course_id], (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows == 1) {
      return res.ok(null, "修改成功")
    }
    return res.cc("出错了")

  })
  // res.ok("ok")


}
const deleteScore = (req, res) => {
  console.log(req.params);
  const { student_id, course_id } = req.params
  const sql = ` delete from t_score where student_id=? and course_id=?`
  db.query(sql, [student_id, course_id], (err, resaults) => {
    if (err) return res.cc(err)
    return res.ok(resaults, "删除成功")
  })
}
module.exports = {
  addScore, updateScore, deleteScore, findAll, selectStudentScore
}